package com.haoniu.web.controller.module;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haoniu.common.annotation.Log;
import com.haoniu.common.base.AjaxResult;
import com.haoniu.common.enums.BusinessType;
import com.haoniu.common.utils.huanxin.HuanXinConstants;
import com.haoniu.framework.util.ShiroUtils;
import com.haoniu.framework.web.page.TableDataInfo;
import com.haoniu.module.domain.database.MerchantChargeRecord;
import com.haoniu.module.domain.database.MerchantInfo;
import com.haoniu.module.domain.database.MerchantPayRecord;
import com.haoniu.module.service.MerchantChargeRecordService;
import com.haoniu.module.service.MerchantInfoService;
import com.haoniu.web.controller.tool.HttpClientUtil;
import com.haoniu.web.core.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户充值金额流水明细 信息操作处理
 *
 * @author houyijun
 * @date 2019-01-28
 */
@Controller
@RequestMapping("/module/merchantChargeRecord")
public class MerchantChargeRecordController extends BaseController
{
    private String prefix = "module/merchantChargeRecord";

	@Autowired
	private MerchantChargeRecordService merchantChargeRecordService;

	@Autowired
	private MerchantInfoService merchantInfoService;

	@RequiresPermissions("module:merchantChargeRecord:view")
	@GetMapping()
	public String merchantChargeRecord(ModelMap mmap)
	{  // 获取商户的信息
		Map<String, Object> params = new HashMap<>();
		params.put("merchantId", HuanXinConstants.MERCHANT_ID);
		try {
			String result = HttpClientUtil.getInstance().doPost(HuanXinConstants.MERCHANT_BALANCE,params);
			JSONObject jsonObject = JSONObject.parseObject(result);

			mmap.addAttribute("merchantInfoCanUseMoney",jsonObject.getString("avalibleMoney"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return prefix + "/merchantChargeRecord";
	}

	/**
	 * 查询商户充值金额流水明细列表
	 */
	@RequiresPermissions("module:merchantChargeRecord:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MerchantChargeRecord merchantChargeRecord,
							  @RequestParam(defaultValue = "1")Integer pageNum,
							  @RequestParam(defaultValue = "10")Integer pageSize) throws  Exception {
		startPage();
		Map<String, Object> params = new HashMap<>();
		params.put("merchantId", HuanXinConstants.MERCHANT_ID);
		params.put("pageNum",pageNum);
		params.put("pageSize",pageSize);

		List<MerchantChargeRecord> list =new ArrayList<MerchantChargeRecord>();
			String result = HttpClientUtil.getInstance().doPost(HuanXinConstants.MERCHANT_CHARGE_RECORD,params);
			JSONObject jsonObject = JSONObject.parseObject(result);
			JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("rows"));
			for (int i = 0; i < jsonArray.size(); i ++){
				JSONObject resultObject = JSONObject.parseObject(jsonArray.getString(i));
				MerchantChargeRecord merchantRecordReq = new MerchantChargeRecord();
				merchantRecordReq.setId(resultObject.getInteger("id"));
				merchantRecordReq.setTransMoney(resultObject.getBigDecimal("transMoney"));
				merchantRecordReq.setBeforeMoney(resultObject.getBigDecimal("beforeMoney"));
				merchantRecordReq.setAfterMoney(resultObject.getBigDecimal("afterMoney"));
				merchantRecordReq.setCreateTime(resultObject.getDate("createTime"));
				merchantRecordReq.setChargeRemark(resultObject.getString("chargeRemark"));
				list.add(merchantRecordReq);
			}
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(0);
		rspData.setRows(list);
		rspData.setTotal(jsonObject.getInteger("total"));
		return rspData;
	}

	/**
	 * 新增商户充值金额流水明细
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}

	/**
	 * 新增保存商户充值金额流水明细
	 */
	@RequiresPermissions("module:merchantChargeRecord:add")
	@Log(title = "商户充值金额流水明细", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MerchantChargeRecord merchantChargeRecord) {
        merchantChargeRecord.setCreateBy(ShiroUtils.getLoginName());
		return toAjax(merchantChargeRecordService.insertMerchantChargeRecord(merchantChargeRecord));
	}

	/**
	 * 修改商户充值金额流水明细
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		MerchantChargeRecord merchantChargeRecord = merchantChargeRecordService.selectMerchantChargeRecordById(id);
		mmap.put("merchantChargeRecord", merchantChargeRecord);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存商户充值金额流水明细
	 */
	@RequiresPermissions("module:merchantChargeRecord:edit")
	@Log(title = "商户充值金额流水明细", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MerchantChargeRecord merchantChargeRecord) {
        merchantChargeRecord.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(merchantChargeRecordService.updateMerchantChargeRecord(merchantChargeRecord));
	}

	/**
	 * 删除商户充值金额流水明细
	 */
	@RequiresPermissions("module:merchantChargeRecord:remove")
	@Log(title = "商户充值金额流水明细", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(merchantChargeRecordService.deleteMerchantChargeRecordByIds(ids));
	}

}
